package bot;

import java.io.IOException;

import org.jibble.pircbot.*;

import twitter4j.*;

public class JmacBot extends PircBot{
	AwfulWaffle bot;
	int twitAt;
	int count;
	Twitter twit;
	
	public JmacBot() {
		this("AwfulWaffle");
	}

	public JmacBot(String botName){
		this.setName(botName);
		bot = new AwfulWaffle();
		twit = new Twitter("TWlTTERSHlTTER","zealot");
		twitAt = 15;
		count = 0;
	}

	public void onMessage(String channel,
			String sender,
			String login,
			String hostname,
			String message) {

		try {
			// Check for non-empty messages and commands
			if (!message.isEmpty() && message.equalsIgnoreCase("!")) {
				String gen = bot.getSentence();
				sendMessage(channel, gen);
				
				// Update to Twitter if applicable 
				if(count >= twitAt){
					twit.updateStatus(gen);	
					count = 0;
					System.out.println("Sent to Twitter: " + gen);
				} 
			
			// Tell AwfulWaffle to read message
			} else if (!message.isEmpty()) {
				bot.readPhrase(message);
				count++;
			}
		} catch (IndexOutOfBoundsException e) {
			// Failure
		} catch (TwitterException e) {
			sendMessage(channel, "TWLTTERSHLTTER update failed!");
			e.printStackTrace();
		}

	}

	public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) {
		if(channel.equals("#jmac"))
			this.joinChannel("#jmac");
	}
	
	public void onDisconnect(){
		try {
			this.reconnect();
		} catch (NickAlreadyInUseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IrcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
